package pl.edu.agh.sit.servlets;

import java.io.IOException;
import java.util.Collection;
import java.util.LinkedHashMap;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pl.edu.agh.sit.logic.Program;
import pl.edu.agh.sit.logic.TvProgramDAO;

import com.google.appengine.repackaged.org.json.JSONObject;

@SuppressWarnings("serial")
public class SuggestionServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		JSONObject json = null;
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		
		try {
			String[] paramValues = req.getParameterValues("brutality");
			int brutality = Integer.parseInt(paramValues[0]);

			paramValues = req.getParameterValues("romantism");
			int romantism = Integer.parseInt(paramValues[0]);

			paramValues = req.getParameterValues("comedy");
			int comedy = Integer.parseInt(paramValues[0]);

			paramValues = req.getParameterValues("startTime");
			int startTime = Integer.parseInt(paramValues[0]);

			paramValues = req.getParameterValues("endTime");
			int endTime = Integer.parseInt(paramValues[0]);

			Collection<Program> programsByPreferences = TvProgramDAO
					.getProgramsByPreferences(brutality, comedy, romantism,
							startTime, endTime);

			Integer i = 0;
			for (Program program : programsByPreferences) {
				map.put(i.toString(), program.title);
				i++;
			}
			map.put("status", "ok"); //when response is correct

			json = new JSONObject(map);
		} catch (Exception e) {
				map.put("status", "error");
				map.put("message", e.toString());
				json = new JSONObject(map);
				
				log(e.getMessage());
		}
		resp.setContentType("text/plain");
		resp.getWriter().println(json.toString());
	}
}
